package 剑指Offer.medium;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.stream.Stream;

public class 把数组排成最小的数 {
    class Solution {
        public String minNumber(int[] nums) {
            Queue<String> pq = new PriorityQueue<>((x, y) -> (x + y).compareTo(y + x));
            for (int num : nums) {
                pq.add(String.valueOf(num));
            }
            StringBuilder res = new StringBuilder();
            while (!pq.isEmpty()) {
                res.append(pq.poll());
            }
            return res.toString();
        }

        public String minNumbe2r(int[] nums) {
            return Arrays.stream(nums).boxed().map(String::valueOf).sorted((x, y) -> (x + y).compareTo(y + x)).reduce("", (a, b) -> a + b);
        }
    }
}
